<template>
  <div class="app-container">
    <!-- 过滤-->
    <div class="filter-container">
      <el-input
        v-model="listQuery.condition.name"
        :placeholder="'用户名'"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.condition.roles"
        :placeholder="'角色'"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option
          v-for="item in calendarTypeOptions"
          :key="item.id"
          :label="item.roleName"
          :value="item.id"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >{{ "搜索" }}</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >{{ "新增" }}</el-button>
      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="handleDownload"
      >{{ "导出" }}</el-button>
    </div>
    <!-- 表格 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @sort-change="sortChange"
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">{{ scope.$index }}</template>
      </el-table-column> 
      <el-table-column label="用户名">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="对应角色" width="180" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.rolesName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createdTime}}</span>
        </template>
      </el-table-column>
      <!-- 操作按钮 -->
      <el-table-column :label="'操作'" align="center" width="230" class-name="fixed-width">
        <template slot-scope="{row, $index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">{{ "编辑" }}</el-button>

          <el-popconfirm
            confirmButtonText="确认"
            cancelButtonText="取消"
            icon="el-icon-info"
            iconColor="red"
            title="确认删除吗?"
            @onConfirm="handleDelete(row, $index)"
            @keyup.enter.native="handleDelete(row, $index)"
          >
            <el-button
              v-if="row.status!=='deleted'"
              size="mini"
              type="danger"
              slot="reference"
            >{{ "删除" }}</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 弹窗(新建编辑) -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="saveRules"
        :model="tempUserData"
        label-position="left"
        label-width="100px"
        autocomplete="on"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item :label="'角色'" prop="roles">
          <el-select v-model="tempUserData.roles" class="filter-item" placeholder="请选择">
            <el-option
              v-for="item2 in calendarTypeOptions"
              :key="item2.id"
              :label="item2.roleName"
              :value="item2.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="'邮箱地址'" prop="email">
          <el-input
            ref="email"
            v-model="tempUserData.email"
            name="email"
            type="text"
            autocomplete="on"
            placeholder="输入邮箱地址"
          />
        </el-form-item>
        <el-form-item :label="'用户名'" prop="name">
          <el-input
            v-model="tempUserData.name"
            name="name"
            type="text"
            style="width: 200px;"
            :disabled="true"
          />
        </el-form-item>

        <el-form-item :label="'密码'" prop="password">
          <el-input
            v-model="tempUserData.password"
            type="password"
            style="width: 200px;"
            placeholder="请输入登录密码"
            name="password"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ "取消" }}</el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >{{"保存" }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { startLoading, endLoading, formatJson } from "@/utils/common/utils";
import { exportJson2Excel } from "@/utils/common/excel";
import { User } from "@/entity/auth/User";
import { Form, Input } from "element-ui";
import Pagination from "@/components/Pagination/index.vue";
import { getUsers, createUser, updateUser, delectUser } from "@/api/auth/user";
import { getRoles } from "@/api/auth/role";
import { Paging } from "@/utils/Type";

const calendarTypeOptions: any = [];
@Component({
  name: "Table",
  components: {
    Pagination,
  },
})
export default class extends Vue {
  private tableKey = 0;
  private list: User[] = [];
  private listLoading = true;
  // 分页对象
  private total = 0;
  private listQuery: Paging = {
    page: 1,
    limit: 10,
    condition: {
      name: "",
      roles: "",
    },
  };
  private downloadLoading = false;
  // 弹窗上的字符
  private dialogStatus = "";
  // 控制弹窗显示
  private dialogFormVisible = false;
  private dialogFormDelete = false;
  private tempUserData: User = new User();
  private textMap = {
    update: "编辑",
    create: "创建",
  };
  //select
  private calendarTypeOptions = calendarTypeOptions;
  private pageviewsData = [];
  private rolesData: any = null;
  /**
   * 绑定表单对应方法和事件
   */
  private saveRules = {
    roles: [{ required: true, message: "必选项", trigger: "change" }],
    email: [{ required: true, message: "必填项", trigger: "blur" }],
    name: [{ required: true, message: "必填项", trigger: "blur" }],
    password: [{ required: true, message: "必填项", trigger: "blur" }],
  };
  /**
   * 生命周期方法
   * 在模板渲染成html前调用，即通常初始化某些属性值，然后再渲染成视图。
   */
  created() {
    this.getList();
  }

  /**
   * 获取用户数据
   */
  private async getList() {
    startLoading(this.listLoading);
    const { data } = await getUsers(this.listQuery);
    if (this.rolesData === null) {
      this.rolesData = await getRoles();
      this.calendarTypeOptions = this.rolesData.data;
    }
    this.list = data.items;
    this.total = data.total;
    // 模拟加载中结束
    this.listLoading = false;
  }
  /**
   * 创建用户
   */
  private handleCreate() {
    // 清理当前对象数据
    this.tempUserData = new User();
    this.dialogStatus = "create";
    this.dialogFormVisible = true;
    this.$nextTick(() => {
      (this.$refs.dataForm as Form).clearValidate();
    });
  }
  /**
   * 搜索
   */
  private async handleFilter() {
    this.listQuery.page = 1;
    startLoading(this.listLoading);
    const { data } = await getUsers(this.listQuery);
    this.list = data.items;
    this.total = data.total;
    // 模拟加载中结束
    this.listLoading = false;
  }
  /**
   * 导出Excel
   */
  private handleDownload() {
    this.downloadLoading = true;
    const tHeader = ["timestamp", "title", "type", "importance", "status"];
    const filterVal = ["timestamp", "title", "type", "importance", "status"];
    const data = formatJson(filterVal, this.list);
    exportJson2Excel(tHeader, data, "table-list");
    this.downloadLoading = false;
  }
  private sortChange(data: any) {
    const { prop, order } = data;
    if (prop === "id") {
    }
  }
  /**
   * 创建用户信息
   */
  private createData() {
    (this.$refs.dataForm as Form).validate(async (valid) => {
      if (valid) {
        const user = this.tempUserData;
        const { data } = await createUser(user);
        data.timestamp = Date.parse(data.timestamp);
        this.list.unshift(data);
        this.dialogFormVisible = false;
        this.$notify({
          title: "成功",
          message: "创建成功",
          type: "success",
          duration: 2000,
        });
      }
    });
  }
  /**
   * 修改用户信息
   */
  private updateData() {
    (this.$refs.dataForm as Form).validate(async (valid) => {
      if (valid) {
        const tempData = Object.assign({}, this.tempUserData);
        const { data } = await updateUser(tempData);
        const index = this.list.findIndex((v) => v.id === data.id);
        this.list.splice(index, 1, data);
        this.dialogFormVisible = false;
        this.$notify({
          title: "成功",
          message: "更新成功",
          type: "success",
          duration: 2000,
        });
      }
    });
  }

  /**
   * 编辑用户信息
   */
  private handleUpdate(row: any) {
    this.tempUserData = Object.assign({}, row);
    this.dialogStatus = "update";
    this.dialogFormVisible = true;
    this.$nextTick(() => {
      (this.$refs.dataForm as Form).clearValidate();
    });
  }

  /**
   * 删除用户信息
   */
  private async handleDelete(row: any, index: number) {
    const { data } = await delectUser(row);
    this.$notify({
      title: "成功",
      message: "删除成功",
      type: "success",
      duration: 200,
    });
    this.list.splice(index, 1);
  }
}
</script>
